package com.newsproject.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.newsproject.mapper.GetAnnouncementMapper;
import com.newsproject.pojo.*;
import com.newsproject.service.GetAnnouncementService;

@Service
public class GetAnnouncementServiceImpl implements GetAnnouncementService {

    @Autowired
    private GetAnnouncementMapper getAnnouncementMapper;

    @Override
    public int getAnnouncementCount(HttpSession session) {
        // 从session中获取个人用户信息
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return 0;
        }

        Date lastCheckTime = (Date) session.getAttribute("lastCheckTime");
        if (lastCheckTime == null) {
            lastCheckTime = new Date(0); // 如果没有记录，默认从1970年开始
        }
        // 时间转为string
        // 时间变成yyyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(lastCheckTime);

        // 新增的私信数量
        int messageCount = getAnnouncementMapper.getPrivateMessageCount(user.getUserId(), time);

        // 新增的评论数量
        int commentCount = getAnnouncementMapper.getCommentCount(user.getUserId(), time);

        // 新增的@我的数量
        int atMeCount = getAnnouncementMapper.getAtMeCount(user.getUserId(), time);

        // 新增的评论点赞数量
        int commentLikeCount = getAnnouncementMapper.getCommentLikeCount(user.getUserId(), time);

        // 新增的新闻点赞数量
        int newsLikeCount = getAnnouncementMapper.getNewsLikeCount(user.getUserId(), time);

        // 粉丝变化数量
        int fansChangeCount = getAnnouncementMapper.getFansChangeCount(user.getUserId(), time);

        // 更新 lastCheckTime
        session.setAttribute("lastCheckTime", new Date());

        return messageCount + commentCount + atMeCount + commentLikeCount + newsLikeCount + fansChangeCount;
    }
}
